<?php
ob_start();
//include('common/db.php');
include('common/xta.php');
include('common/xpath.php');

$xta = new xta();
$xp = new xp();

$stock_code = $_GET['code'];

$all_moves = array();

if(!empty($stock_code)) {
	$xta->load($stock_code."_monthly");		
	$all_moves = $xta->data['months'];	
}

//print_r($xta->data);
array_shift($all_moves);
//print_r($all_moves);

$sorted = array();
foreach($all_moves as $key => $each_move) {
	$timestamp = strtotime("01 ".$each_move['month']);	
	$sorted[$timestamp] = $key;
}
krsort($sorted);
//print_r($sorted);

if(isset($_GET['ajax'])) {
	$data = array();
	foreach($all_moves as $each_move) {
		$timestamp = strtotime("01 ".$each_move['month']);
		$data[$timestamp] = $each_move['close_price'];	
	}
	
	ksort($data);
	
	if(count($data) > 0)
		echo implode(",", $data);
	else
		echo "0";
	exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
<link rel="stylesheet" type="text/css" href="jquery.tablesorter/themes/blue/style.css"></link>
<script type="text/javascript" src="jquery.tablesorter/jquery-latest.js"></script>
<script type="text/javascript" src="jquery.tablesorter/jquery.tablesorter.js"></script>
<script type="text/javascript">
	$(function() {
		$("#companies").tablesorter();
	});
</script>
<body>
<table id="companies" cellspacing="0" class="tablesorter">
	<thead>
		<tr>
			<th>Date</th>
			<th>Open</th>
			<th>Close</th>
			<th>Heigh</th>
			<th>Low</th>			
		</tr>
	</thead>
	<tfoot>
		<tr>
			<th>Date</th>
			<th>Open</th>
			<th>Close</th>
			<th>Heigh</th>
			<th>Low</th>
		</tr>
	</tfoot>
	<tbody>
	<?php foreach($sorted as $each_order) { $each_move = $all_moves[$each_order];?>		
	<tr>
		<td><?=$each_move['month']?></td>
		<td><?=$each_move['open_price']?></td>
		<td><?=$each_move['close_price']?></td>
		<td><?=$each_move['height_price']?></td>
		<td><?=$each_move['low_price']?></td>
	</tr>
	<?php } ?>
	</tbody>
</table>
</body>
</html>